﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Altbrot.Yawr.Rss;
using Altbrot.Yawr.ShoutCast;
using System.Reflection;
using System.ComponentModel;
using Altbrot.Yawr.Buslogic;
using Altbrot.Yawr.Entities;

namespace ShoutCastManagerTest
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window, INotifyPropertyChanged
    {
        private ShoutCastManager _shoutCastManager;
        private readonly string _baseUri = "http://www.shoutcast.com/sbin/newxml.phtml?rss=1";
        //private readonly string _baseUri = "http://localhost/base.xml";
        private readonly string _tmpFilePath = "tunein-station.pls";
        private FeedItem _selectedFeedItem;
        private List<FeedItem> _feedItemList;
        private Guid _currentTaskId;

        public FeedItem SelectedFeedItem
        {
            get
            {
                return _selectedFeedItem;
            }
            set
            {
                _selectedFeedItem = value;
                _shoutCastManager.SelectedFeedItem = _selectedFeedItem;
            }
        }

        public List<FeedItem> FeedItemList
        {
            get
            {
                return _feedItemList;
            }
            set
            {
                _feedItemList = value;
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs("FeedItemList"));
            }
        }

        public Window1()
        {
            InitializeComponent();

            string filePath = System.IO.Path.Combine(
                System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), _tmpFilePath);
            Uri baseUri = new Uri(_baseUri);

            //string connectionString = @"C:\Users\altbrot\work\carpc\ModuleDevelopment\Yawr\Altbrot.Yawr.Plugin\Yawr\Database\FeedItemDb.sdf";
            //string connectionString = @"C:\Projects\Yawr\Altbrot.Yawr.Plugin\Yawr\Database\FeedItemDb.sdf";
            string connectionString = @"C:\Projects\Yawr\Altbrot.Yawr.Plugin\Database\FeedItemDb.sdf";

            //_shoutCastManager = new ShoutCastManager(baseUri, filePath, new OnlineBuslogicManager());
            _shoutCastManager = new ShoutCastManager(baseUri, filePath, new DatabaseBuslogicManager(connectionString));
            _shoutCastManager.GetBaseStepCompleted += new GetStepCompletedEventHandler(ShoutCastManager_GetStepCompleted);
            _shoutCastManager.GetNextStepCompleted += new GetStepCompletedEventHandler(ShoutCastManager_GetStepCompleted);
            _shoutCastManager.GetPrevStepCompleted += new GetStepCompletedEventHandler(ShoutCastManager_GetStepCompleted);
            _shoutCastManager.UpdateCompleted += new UpdateCompletedEventHandler(_shoutCastManager_UpdateCompleted);
            _currentTaskId = _shoutCastManager.GetBaseStepAsync();

            SetEnabled(false);

            this.lbFeedItems.DataContext = this;
        }

        private void ShoutCastManager_GetStepCompleted(object sender, GetStepCompletedEventArgs e)
        {
            if (e.Error == null && e.FeedList != null && e.FeedList.Count > 0)
                FeedItemList = e.FeedList;

            if (FeedItemList != null && FeedItemList.Count > 0)
                this.lbFeedItems.SelectedIndex = 0;

            SetEnabled(true);
        }

        void _shoutCastManager_UpdateCompleted(object sender, UpdateCompletedEventArgs e)
        {
            if (e.Error == null && e.FeedList != null && e.FeedList.Count > 0)
                FeedItemList = e.FeedList;

            if (FeedItemList != null && FeedItemList.Count > 0)
                this.lbFeedItems.SelectedIndex = 0;

            SetEnabled(true);
        }

        private void SetEnabled(bool state)
        {
            this.btnNext.IsEnabled = state;
            this.btnPrev.IsEnabled = state;
            this.btnHome.IsEnabled = state;
            this.btnSearch.IsEnabled = state;
            this.btnUpdate.IsEnabled = state;
        }

        private void btnPrev_Click(object sender, RoutedEventArgs e)
        {
            _shoutCastManager.GetPrevStepAsync();
            SetEnabled(false);
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            _shoutCastManager.GetNextStepAsync();
            SetEnabled(false);
        }

        private void btnHome_Click(object sender, RoutedEventArgs e)
        {
            _shoutCastManager.GetBaseStepAsync();
            SetEnabled(false);
        }

        private void btnPlay_Click(object sender, RoutedEventArgs e)
        {
            _shoutCastManager.PlayUrl();
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            _shoutCastManager.Stop();
        }

        private void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            _shoutCastManager.UpdateAsync();
            SetEnabled(false);
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            List<FeedItem> sl = _shoutCastManager.Search(this.tbSearch.Text);
            if (sl != null && sl.Count > 0)
                FeedItemList = sl;

            if (FeedItemList != null && FeedItemList.Count > 0)
                this.lbFeedItems.SelectedIndex = 0;
        }

        private void btnAddFav_Click(object sender, RoutedEventArgs e)
        {
            _shoutCastManager.ToggleFavorite();
        }

        private void btnFavs_Click(object sender, RoutedEventArgs e)
        {
            List<FeedItem> sl = _shoutCastManager.GetFavoritesList();
            if (sl != null && sl.Count > 0)
                FeedItemList = sl;

            if (FeedItemList != null && FeedItemList.Count > 0)
                this.lbFeedItems.SelectedIndex = 0;
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
